package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import com.huike.review.vo.MybatisReviewVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**
     * 新增接口1,插入数据
     * @param name
     * @param age
     * @param sex
     * @return
     */
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveData1(@PathVariable("name") String name,
                               @PathVariable("age") Integer age,
                               @PathVariable("sex") String sex){
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        reviewService.saveData1(review);
        return AjaxResult.success("成功插入数据");
    }

    /**
     * 新增接口2,插入数据
     * @param mybatisReviewVO
     * @return
     */
    @PostMapping("/saveData")
    public AjaxResult saveData2(@RequestBody MybatisReviewVO mybatisReviewVO){
        Review review = new Review();
        BeanUtils.copyProperties(mybatisReviewVO,review);
        reviewService.saveData2(review);
        return AjaxResult.success("成功插入数据");
    }

    /**
     * 修改数据
     * @param mybatisReviewVO
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MybatisReviewVO mybatisReviewVO){
        Review review = new Review();
        BeanUtils.copyProperties(mybatisReviewVO,review);
        reviewService.update(review);
        return AjaxResult.success("修改成功");
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable("id") long id){
        reviewService.remove(id);
        return AjaxResult.success("删除成功");
    }

    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public AjaxResult getById(long id){
        List<Review> list = reviewService.getById(id);
        List<MybatisReviewVO> list1 = list.stream().map((review) ->{
            MybatisReviewVO mybatisReviewVO = new MybatisReviewVO();
            BeanUtils.copyProperties(review,mybatisReviewVO);
            return mybatisReviewVO;
        }).collect(Collectors.toList());
        return AjaxResult.success("操作成功",list1);
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/getDataByPage")
    public TableDataInfo getDataByPage(MybatisReviewVO mybatisReviewVO){
        startPage();
        Review review = new Review();
        BeanUtils.copyProperties(mybatisReviewVO,review);
        List<Review> list = reviewService.getDataByPage(review);
        List<MybatisReviewVO> reviewVOS = list.stream().map((item) -> {
            MybatisReviewVO mybatisReviewVO1 = new MybatisReviewVO();
            BeanUtils.copyProperties(item,mybatisReviewVO1);
            return mybatisReviewVO1;
        }).collect(Collectors.toList());
        return getDataTablePage(reviewVOS);
    }

}